package com.pet.system.controller;

import com.pet.common.constant.ResponseCode;
import com.pet.common.model.PageModel;
import com.pet.common.model.ResponseResult;
import com.pet.system.domain.entity.SysRole;
import com.pet.system.domain.vo.SysRoleVO;
import com.pet.system.service.ISysRoleService;
import com.pet.system.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色控制器
 *
 * <p>1、角色分页查询（角色列表） 2、全部角色查询（用于角色分配时下拉场景） 3、添加角色、修改角色、删除角色（角色自身信息 + 关联资源信息）
 *
 * @author Centaurea
 */
@Api(tags = "角色管理", description = "用户CRUD等功能")
@RestController
@RequestMapping("role")
@AllArgsConstructor
public class RoleController {
  private final ISysRoleService roleService;

  /**
   * 分页查询 角色列表
   *
   * @return xx
   */
  @ApiOperation("角色信息")
  @GetMapping
  @RequiresPermissions("sys:role:list")
  public ResponseResult query(Integer pageNo, Integer pageSize, String searchWord, String status) {
    Map<String, Object> params = new HashMap<>(2);
    params.put("searchWord", searchWord);
    params.put("status", status);
    List<SysRole> sysRoles =
        roleService.queryAllByPage(new PageModel<SysRole>(pageNo, pageSize), params);
    return ResponseResult.success(ResponseCode.WEB_200, sysRoles);
  }

  /**
   * 查询全部 角色
   *
   * @return xx
   */
  @ApiOperation("全部角色信息")
  @GetMapping("/all")
  public ResponseResult queryAll() {
    List<SysRole> sysRoles = roleService.queryAll(new HashMap<>(0));
    return ResponseResult.success(ResponseCode.WEB_200, sysRoles);
  }

  @ApiOperation("角色信息转字典")
  @GetMapping("/dictionary")
  public ResponseResult dictionary(Map<String, Object> params) {
    List<SysRole> list = roleService.queryAll(params);
    List<Map<String, Object>> res = new ArrayList<>();
    list.forEach(
        record -> {
          Map<String, Object> item = new HashMap<>(2);
          item.put("value", record.getId());
          item.put("label", record.getName());
          res.add(item);
        });
    return ResponseResult.success(ResponseCode.WEB_200, res);
  }

  /**
   * 添加角色（包含分配资源权限）
   *
   * @param roleDto xx
   * @return xx
   */
  @ApiOperation("新增角色")
  @PostMapping
  @RequiresPermissions("sys:role:save")
  public ResponseResult save(SysRoleVO roleDto) {
    try {
      roleService.save(roleDto);
    } catch (DuplicateKeyException e) {
      return ResponseResult.error(roleDto.getRoleKey() + "已存在，不可重复添加");
    }
    return ResponseResult.success(ResponseCode.WEB_200);
  }

  @ApiOperation("修改角色")
  @PutMapping
  @RequiresPermissions("sys:role:update")
  public ResponseResult update(SysRoleVO roleDto) {
    roleService.save(roleDto);
    return ResponseResult.success(ResponseCode.WEB_200);
  }

  @ApiOperation("修改角色状态")
  @PutMapping("/updateStatus")
  @RequiresPermissions({"sys:role:disabled", "sys:role:useable"})
  public ResponseResult updateStatus(Integer id, String status) {
    if (id == null || id < 0) {
      return ResponseResult.error("参数不合法");
    }
    boolean b = roleService.updateStatus(id, Integer.valueOf(status));
    return ResponseResult.success(ResponseCode.WEB_200, b);
  }

  /**
   * 删除角色
   *
   * @return xx
   */
  @ApiOperation("删除角色")
  @DeleteMapping("/{ids}")
  @RequiresPermissions("sys:role:delete")
  public ResponseResult del(@PathVariable("ids") String ids) {
    roleService.delete(StringUtil.parseInteger(ids));
    return ResponseResult.success(ResponseCode.WEB_200);
  }
}
